<?php

/**
 * @category   MM
 * @package    MM_Service
 */
class MM_Service_Admin_EventGuests extends Mediad2_Service_Abstract
{
	/**
	 * 初始化
	 */
	public function init()
	{
		$this->setMapper('MM_Mapper_EventGuests');
		$this->setModel('MM_Model_EventGuests');
	}
	
	/**
	 * 为活动选择嘉宾
	 * @param int $uid
	 * @param string $gids
	 * @param int $eid
	 * @param int $esid
	 * @return Mediad2_Model_Result
	 */
	public function chooseGuests($uid, $gids, $eid, $esid=0)
	{
		try {
			$arrGid = explode(',', $gids);
			foreach ($arrGid as $key => $gid) {
				//获得指定的guests
				$serviceGuests = new MM_Service_Admin_Guests();
				$where = "uid = $uid AND gid = $gid";
				$result = $serviceGuests->findBy($where);
				if ($result->getCode()==$result::CODE_FAILURE) return $result;
				$guests = $result->getData();
			
				//封装event guests model
				$eventGuests = new MM_Model_EventGuests();
				$eventGuests->setEid($eid);
				$eventGuests->setEsid($esid);
				$eventGuests->setGid($gid);
				$eventGuests->setStatus($eventGuests::STATUS_INVITE);
				$eventGuests->setAddtime(time());
				$eventGuests->setInvitetime(0);		//邀请时间
				$eventGuests->setNum(0);			//顺序
				$eventGuests->setName($guests->getName());
				$eventGuests->setPinyin($guests->getPinyin());
				$eventGuests->setCompany($guests->getCompany());
				$eventGuests->setPosition($guests->getPosition());
				$eventGuests->setEmail($guests->getEmail());
				$eventGuests->setQq($guests->getQq());
				$eventGuests->setMobile($guests->getMobile());
				$eventGuests->setWebsite($guests->getWebsite());
				$eventGuests->setDesc($guests->getDesc());
				$eventGuests->setSinaweibo($guests->getSinaweibo());
				$eventGuests->setSinanickname($guests->getSinanickname());
				$eventGuests->setQqweibo($guests->getQqweibo());
				$eventGuests->setQqnickname($guests->getQqnickname());
				$eventGuests->setAvatar($guests->getAvatar());
			
				//保存event guests model
				$this->getMapper()->insert($eventGuests->toArray());
			}
			return $this->returnResult(Mediad2_Model_Result::CODE_SUCCESS); //返回结果--成功
		} catch (Zend_Exception $e) {
			$this->handleException($e); //出错处理
			return $this->returnResult(Mediad2_Model_Result::CODE_FAILURE); //返回结果--失败
		}
		
	}
	
	/**
	 * 移除选择的嘉宾
	 * @param int $uid
	 * @param string $gids
	 * @param int $eid
	 * @param int $esid
	 * @return Mediad2_Model_Result
	 */
	public function removeGuests($uid, $gids, $eid, $esid=0)
	{
		$where = "uid = $uid AND eid = $eid AND gid in ($gids)";
		if ($esid<>0) $where .= " AND esid = $esid";
		return $this->deleteBy($where);
	}
	
}
